package com.cn.why.film.filmindex.service.serviceImpl;


import com.cn.why.film.filmindex.entity.Produce;
import com.cn.why.film.filmindex.repository.ProduceRepository;
import com.cn.why.film.filmindex.service.ProducesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 实现层
 */
@Service
public class ProducesServiceImpl implements ProducesService {

    //自动装配dao
    @Autowired
//    @Qualifier(value = "proDao")当接口有2个实现类以上时，可以用该注解来指定调用哪个接口
    private ProduceRepository proDao;
    @Autowired
    private SimpMessagingTemplate template;

    //创建一个线程池
    private static ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 11, 10, TimeUnit.SECONDS, new LinkedBlockingDeque<>(1000));

    @Override
    public List<Produce> getProList() {
        return proDao.findAll();
    }

    @Override
    public Produce findProById(Integer id) {
        return proDao.findOne(id);
    }

    @Override
    public void save(Produce produces) {
        proDao.save(produces);
    }

    @Override
    public void edit(Produce produces) {
        proDao.save(produces);
    }

    @Override
    public void delete(Integer id) {
        proDao.delete(id);
    }

    @Override
    public void forAddProduces() {
        for (int i = 0; i < 100; i++) {
            final Long proNumber = (long) i;
            final String proName = i + "";
            final double price = i;
            final Long number = (long) i;
            Runnable task = new Runnable() {
                @Override
                //批量添加
                public void run() {
                    Produce produce = new Produce(proNumber, proName, price, number);
                    proDao.save(produce);
                }
            };
            executor.execute(task);
        }
    }
}

